package com.partner.partnerback.common;

import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.http.HttpUtil;
import com.partner.partnerback.entity.User;
import com.partner.partnerback.mapper.UserMapper;
import com.partner.partnerback.service.IUserService;
import com.partner.partnerback.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
@Slf4j
public class InitRunner implements ApplicationRunner {

    @Resource
    UserMapper userMapper;

    /**
     * 在项目成功之后会运行这个方法
     * @param args
     * @throws Exception
     */
    @Override
    public void run(ApplicationArguments args) throws Exception {


        ThreadUtil.execAsync(() -> {
            try{
                // redis数据库探测，初始化连接
                RedisUtils.ping();
                //再项目启动时查询一次数据库，防止数据库懒加载 //我没有遇到这个问题，写这个只是为了防止出现
                userMapper.select1();
                log.info("项目启动数据库查询成功");
                HttpUtil.get("http://localhost:9090/");
                log.info("启动项目tomcat连接查询");
            }catch (Exception e){
                log.warn("启动优化失败",e);
            }
        });

    }
}
